<!-- 券专区订单列表 -->
<template>
	<view class="container">
		<uni-nav-bar left-icon="back" status-bar="true" @clickLeft="goBack" title="我的订单"></uni-nav-bar>
		
		<view class="content">
			<!-- 空白页 -->
			<empty v-if="emptyShow" :custom="true" src="../../static/images/no-orders.png" text="您还没有相关订单哦 快去前往消费"></empty>
			<!-- 订单列表 -->
			<navigator v-for="(items,index) in list" :key="index" :url="'/mall/Ticket/IntegralOrderDetail?item=' + encodeURIComponent(JSON.stringify(items))"
			 hover-class="none">
				<view class="order-item">
					<view class="i-top b-b">
						<!-- <image class="seller_logo" :src="items.online_logo" mode=""></image> -->
						<text class="seller_name clamp">{{items.ticket_shop_name}}</text>
						<text class="state">{{items.status_desc}}</text>
					</view>
					<scroll-view v-if="items.products.length > 1" class="goods-box" scroll-x>
						<view v-for="(goodsItem, goodsIndex) in items.products" :key="goodsIndex" class="goods-item">
							<image class="goods-img" :src="goodsItem.image" mode="aspectFill"></image>
						</view>
					</scroll-view>
					<view v-if="items.products.length === 1" class="goods-box-single" v-for="(goodsItem, goodsIndex) in items.products"
					 :key="goodsIndex">
						<image class="goods-img" :src="goodsItem.image" mode="aspectFill"></image>
						<view class="right">
							<text class="title clamp2">{{goodsItem.store_name}}</text>
							<view class="attr-box">
								<text class="clamp" v-if="goodsItem.sku">规格：{{goodsItem.sku}}</text>
								<text>数量：x {{goodsItem.cart_num}}</text>
							</view>
						</view>
					</view>
					<!-- 待支付 -->
					<view class="action-box b-t" v-if="items.order_status == 0">
						<button class="action-btn" @tap.stop="phoneCall">联系客服</button>
						<button class="action-btn" @tap.stop="cancel(items)">删除订单</button>
						<button class="action-btn act" @tap.stop="checkMoney(items)">立即支付</button>
						<view class="price"><text>券：</text>{{items.integral_price}}</view>
					</view>
					<!-- 待发货 -->
					<view class="action-box b-t" v-if="items.order_status == 1">
						<button class="action-btn" @tap.stop="phoneCall(items.online_phone)">联系客服</button>
						<view class="price"><text>券：</text>{{items.integral_price}}</view>
					</view>
					<!-- 待收货 -->
					<view class="action-box b-t" v-if="items.order_status == 2">
						<button class="action-btn" @tap.stop="phoneCall(items.online_phone)">联系客服</button>
						<button class="action-btn" @tap.stop="express(items.order_id)">查看物流</button>
						<button class="action-btn act" @tap.stop="queren(items.order_id)">确认收货</button>
						<view class="price"><text>券：</text>{{items.integral_price}}</view>
					</view>
					<!-- 已完成 -->
					<view class="action-box b-t" v-if="items.order_status == 3">
						<button class="action-btn" @tap.stop="phoneCall(items.online_phone)">联系客服</button>
						<button class="action-btn" v-if="items.type == 2" @tap.stop="express(items.order_id)">查看物流</button>
						<view class="price"><text>券：</text>{{items.integral_price}}</view>
					</view>
				</view>
			</navigator>

			<uni-load-more class="loadMore" iconType="circle" :status="loadStatus" :contentText="contentText"></uni-load-more>
		</view>
		<!-- app选择支付方式 -->
		<choosePay ref="choosePay" @confirm="confirm"></choosePay>
		<!-- 抵扣全部金额输入平台操作密码 -->
		<passkeyborad class="passkeyborad" :show="keyboradShow" @close="keyboradClose" @finish="finished"></passkeyborad>
		
		<footers></footers>
	</view>
</template>

<script>
	import {
		ticketRecord,
		ticketOrder2,
		ticketConfirmProductOrder,
		ticketCancelOrder
	} from '@/api/mall.js'
	import {
		checkPassword
	} from "@/api/user.js"
	import passkeyborad from '@/components/yzc-paykeyboard/yzc-paykeyboard.vue'
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from "@/components/empty/empty";
	import utils from "@/common/utils.js"
	import wechat from '@/common/wechat.js'
	import choosePay from '@/components/choosePay/choosePay.vue'
	import footers from '@/components/shopFooter/shopFooter.vue'
	export default {
		components: {
			uniLoadMore,
			empty,
			passkeyborad,
			footers
		},
		data() {
			return {
				emptyShow: false,
				limit: 6,
				page: 1,
				keys: '',
				list: [],
				loadStatus: "loading",
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: ""
				},
				paytype: "weixin",
				provider: "wxpay",
				// #ifdef MP-WEIXIN
				payStatus: "routine",
				// #endif
				// #ifdef APP-PLUS
				payStatus: "app",
				// #endif
				// #ifdef H5
				payStatus: "weixin",
				// #endif
				keyboradShow: false,
				order_id: ''
			};
		},
		onLoad(option) {
			this.getList()
		},
		onReachBottom() {
			if (this.loadStatus == "more") {
				this.loadStatus = "loading"
				this.getList()
			}
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack()
			},
			
			//获取兑换记录
			getList() {
				ticketRecord({
					type: 1,
					page: this.page,
					limit: this.limit
				}).then(res => {
					uni.hideLoading()
					this.loadStatus = "more";
					this.list.push.apply(this.list, res.data);
					if (res.data.length < this.limit) this.loadStatus = "noMore"; //判断所有数据是否加载完成；
					this.page = this.page + 1;
					if (this.list.length == 0) {
						this.contentText.contentnomore = ''
						this.emptyShow = true
					} else {
						this.contentText.contentnomore = '没有更多了'
					}
					// this.list = [{

					// 	"id": 23,
					// 	"order_id": "2020110210063981764776（兑换订单列表数据）",
					// 	"integral_price": "79.00",
					// 	"pay_price": "0.01",
					// 	"freight_price": "0.01",
					// 	"number": 1,
					// 	"order_status": 0,
					// 	"status_desc": "未支付",
					// 	"integral_shop_name": "券商城",
					// 	"products": [{
					// 		"image": "http://dev.chinahuishe.com/uploads/attach/2020/10/22/063c27a4a90649f3265838a43f3281b5.jpg",
					// 		"store_name": "杉城休闲零食大礼包一整箱送女友女生儿童礼盒辣条豆干美食品超市网红好吃的组合装1500g",
					// 		"cart_num": 1,
					// 		"price": "79.00",
					// 		"sku": "买80份送80份共计160包"
					// 	}]

					// }]
				})
			},
			checkMoney(item) {
				this.order_id = item.order_id
				if (item.pay_price > 0) {
					//#ifndef APP-PLUS
					this.goPay(item.order_id)
					//#endif
					//#ifdef APP-PLUS
					this.$refs.choosePay.show()
					//#endif
				} else {
					this.keyboradShow = true
				}
			},
			finished(password) {
				uni.showLoading({
					mask: true
				})
				checkPassword({
					password: password
				}, this.keyboradClose()).then(res => {
					this.goPay(this.order_id)
				})
			},
			//选择支付方式
			confirm(e) {
				this.paytype = e.payType
				this.provider = e.provider
				this.goPay(this.order_id)
			},
			// 立即支付
			goPay(id) {
				uni.showLoading({
					mask: true
				})
				let _this = this
				ticketOrder2({
					orderId: id,
					payType: this.paytype,
					from: this.payStatus
				}).then(res => {
					if (res.data.status == "SUCCESS") {
						uni.showToast({
							title: "支付成功"
						})
						_this.loadStatus = "loading";
						_this.emptyShow = false
						_this.page = 1;
						_this.list = [];
						_this.getList();
					} else if (res.data.status == "WECHAT_PAY" || res.data.status == "ALIPAY") {
						// #ifdef MP-WEIXIN
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.result.jsConfig.timestamp,
							nonceStr: res.data.result.jsConfig.nonceStr,
							package: res.data.result.jsConfig.package,
							signType: res.data.result.jsConfig.signType,
							paySign: res.data.result.jsConfig.paySign,
							success: function(re) {
								uni.hideLoading()
								_this.loadStatus = "loading";
								_this.emptyShow = false
								_this.page = 1;
								_this.list = [];
								_this.getList();
							},
							fail(r) {
								console.log(r)
							}
						});
						// #endif
						// #ifdef APP-PLUS
						uni.requestPayment({
							provider: this.provider,
							orderInfo: res.data.result.jsConfig,
							success: (e) => {
								uni.hideLoading()
								_this.loadStatus = "loading";
								_this.emptyShow = false
								_this.page = 1;
								_this.list = [];
								_this.getList();
							}
						})
						// #endif
						// #ifdef H5
						// h5支付
						let url = "/mall/IntegralMall/IntegralOrder"
						wechat.pay(res.data.result.jsConfig, url, false)
						// #endif
					}
				})
			},
			// 确认收货
			queren(id) {
				let _this = this
				uni.showLoading({
					mask: true
				})
				ticketConfirmProductOrder({
					id: id
				}).then(res => {
					uni.hideLoading()
					_this.loadStatus = "loading";
					_this.page = 1;
					_this.list = [];
					_this.getList();
				})
			},
			//联系客服
			phoneCall(phone) {
				uni.makePhoneCall({
					phoneNumber: uni.getStorageSync('servicePhone')
				})
			},
			cancel(item) {
				uni.showLoading({
					mask: true
				})
				ticketCancelOrder({
					order_id: item.order_id
				}).then(res => {
					uni.hideLoading()
					this.list = []
					this.page = 1
					this.loadStatus = "loading"
					this.emptyShow = false
					this.getList()
					uni.showToast({
						title: "取消成功"
					})
				})
			},
			// 查看物流
			express(id) {
				uni.navigateTo({
					url: "/mall/order/express?id=" + id + "&type=jifen"
				})
			},
			keyboradClose() {
				this.keyboradShow = false
			},

		},
		onHide() {
			this.keyboradShow = false
		}
	}
</script>
<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.container {
		.uni-navbar {
			/deep/ .uni-icons {
				span {
					color: #81DF2D;
				}
			}
		}
		
		.content {
			.order-item {
				display: flex;
				flex-direction: column;
				margin: 20rpx 30rpx;
				padding: 0 20rpx;
				background: #fff;
				border-radius: 10rpx;

				.i-top {
					display: flex;
					align-items: center;
					height: 80rpx;
					padding-right: 30rpx;
					font-size: 28rpx;
					color: #303133;
					position: relative;

					.seller_logo {
						width: 40rpx;
						height: 40rpx;
						border-radius: 20rpx;
						margin-right: 20rpx;
					}

					.seller_name {
						font-size: 32rpx;
						font-weight: 500;
						color: #343434;
						max-width: 400rpx;
					}

					.icon-you {
						margin-top: 4rpx;
						margin-left: 10rpx;
						font-size: 24rpx;
					}

					.state {
						position: absolute;
						right: 0;
						top: 24rpx;
						color: #FBC866;
					}
				}

				.cardInfo {
					display: flex;

					.a {
						display: flex;
						align-items: baseline;
						color: #666666;

						.a-a {
							font-size: 28rpx;
						}

						.a-b {
							font-size: 36rpx;
							font-weight: bold;
						}
					}

					.b {
						margin-left: 20rpx;
					}
				}

				/* 多条商品 */
				.goods-box {
					height: 120rpx;
					padding: 20rpx;
					width: 100%;
					margin-left: -20rpx;
					white-space: nowrap;
					background-color: #f8f8f8;

					.goods-item {
						width: 120rpx;
						height: 120rpx;
						display: inline-block;
						margin-right: 24rpx;
					}

					.goods-img {
						display: block;
						width: 100%;
						height: 100%;
					}
				}

				/* 单条商品 */
				.goods-box-single {
					display: flex;
					padding: 20rpx;
					margin-left: -20rpx;
					width: 100%;
					background-color: #f8f8f8;

					.goods-img {
						display: block;
						width: 120rpx;
						height: 120rpx;
					}

					.right {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						padding: 0 0 0 24rpx;
						overflow: hidden;

						.title {
							text-align: justify;
							font-size: 28rpx;
							color: #303133;
							height: 88rpx;
							line-height: 44rpx;
							overflow: hidden;
						}

						.attr-box {
							font-size: 24rpx;
							color: #909399;
							display: flex;

							text:nth-of-type(1) {
								flex: 1;
								margin-right: 50rpx;
							}
						}
					}
				}

				.action-box {
					display: flex;
					align-items: center;
					height: 100rpx;
					position: relative;
				}

				.action-btn {
					width: 130rpx;
					height: 50rpx;
					margin: 0;
					margin-right: 24rpx;
					padding: 0;
					text-align: center;
					line-height: 50rpx;
					font-size: 24rpx;
					color: #666;
					background: #fff;
					border-radius: 100px;


					&:after {
						border-radius: 100px;
					}
				}

				.act {
					background: linear-gradient(90deg, #FBCC2C 0%, #FBDD73 100%);
				}

				.price {
					position: absolute;
					right: 0;
					margin: 0;
					font-size: 30rpx;
					color: #333;

					text {
						font-size: 24rpx;
						color: #999;
					}

					.right {
						margin-left: 14rpx;
					}
				}
			}
		}
	}
</style>
